#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
    IplImage *test;
    char result[300]; //存放预测结果

    CvSVM svm;
    svm.load("d:\\HOG_SVM_DATA.xml");//加载训练好的xml文件，这里训练的是10K个手写数字
    //检测样本
    test = cvLoadImage("d:\\test.bmp", 1); //待预测图片，用系统自带的画图工具随便手写
    if (!test)
    {
        cout<<"not exist"<<endl;
        return -1;
    }
    cout<<"load image done"<<endl;
    IplImage* trainTempImg=cvCreateImage(cvSize(28,28),8,3);
    cvZero(trainTempImg);
    cvResize(test,trainTempImg);
    HOGDescriptor *hog=new HOGDescriptor(cvSize(28,28),cvSize(14,14),cvSize(7,7),cvSize(7,7),9);
    vector<float>descriptors;//存放结果
    hog->compute(trainTempImg, descriptors,Size(1,1), Size(0,0)); //Hog特征计算
    cout<<"HOG dims: "<<descriptors.size()<<endl;  //打印Hog特征维数  ，这里是324
    CvMat* SVMtrainMat=cvCreateMat(1,descriptors.size(),CV_32FC1);
    int n=0;
    for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
    {
        cvmSet(SVMtrainMat,0,n,*iter);
        n++;
    }

    int ret = svm.predict(SVMtrainMat);//检测结果
    sprintf(result, "%d\r\n",ret );
    cvNamedWindow("dst",1);
    cvShowImage("dst",test);
    cout<<"result:"<<result<<endl;
    waitKey ();
    cvReleaseImage(&test);
    cvReleaseImage(&trainTempImg);

    return 0;
}
